//------------------------------------------------------------------------------
// <copyright file="TreeViewProperties.cs" company="Damian Antonowicz">
//       copyright © 2010 Damian Antonowicz
// </copyright>
// <author>Damian Antonowicz</author>
// <email>poczta@damianantonowicz.pl</email>
//------------------------------------------------------------------------------

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Diagnostics;

namespace DotBeer.UI.WPFClient
{
    public static class TreeViewProperties
    {
        public static readonly DependencyProperty SelectedItemProperty =
            DependencyProperty.RegisterAttached("SelectedItem",
                                                typeof(object),
                                                typeof(TreeViewProperties));

        public static object GetSelectedItem(DependencyObject d)
        {
            return d.GetValue(SelectedItemProperty);
        }

        public static void SetSelectedItem(DependencyObject d, object value)
        {
            d.SetValue(SelectedItemProperty, value);
        }

        public static readonly DependencyProperty UseSelectedItemProperty =
            DependencyProperty.RegisterAttached("UseSelectedItem",
                                                typeof(bool),
                                                typeof(TreeViewProperties),
                                                new PropertyMetadata(UseSelectedItem_Changed));

        public static object GetUseSelectedItem(DependencyObject d)
        {
            return d.GetValue(UseSelectedItemProperty);
        }

        public static void SetUseSelectedItem(DependencyObject d, bool value)
        {
            d.SetValue(UseSelectedItemProperty, value);
        }

        private static void UseSelectedItem_Changed(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            if (d is TreeView)
            {
                TreeView treeView = d as TreeView;
                treeView.SelectedItemChanged += (sender, args) =>
                    {
                        treeView.SetValue(SelectedItemProperty, treeView.SelectedItem);
                    };
            }
        }
    }
}
